package com.eboss2.sbs.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.eboss2.sbs.R;
import com.eboss2.sbs.activity.base.BaseActivity;
import com.eboss2.sbs.adapter.CategoryListAdapter;
import com.eboss2.sbs.config.Config;
import com.eboss2.sbs.http.HttpUtil;
import com.eboss2.sbs.model.CategoryListInfo;
import com.eboss2.sbs.model.OrderManagerInfo;
import com.eboss2.sbs.myapplication.MyApplication;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.loopj.android.http.RequestParams;
import com.mytools.tool.activityManager.MyActivityManager;
import com.mytools.tool.utils.StringUtil;
import com.mytools.tool.utils.StringUtils;
import com.mytools.tool.views.MyListView;
import com.uuzuche.lib_zxing.activity.CodeUtils;

import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class MyCategoryListActivity extends BaseActivity implements View.OnClickListener{
	
	private CategoryListInfo vo;
	private List<CategoryListInfo> categoryListInfos = new ArrayList<>();
	private MyListView listview;
	private CategoryListAdapter categoryListAdapter;
	private TextView tv_category_text;

	private int type;

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext = this;
		setContentView(R.layout.activity_category_choice);
		MyActivityManager.getInstance().addActivity(this);
		initView();
		loadData();
//		testData();
	}

	private void testData() {
		for(int i = 0; i<5; i++){
			CategoryListInfo categoryListInfo = new CategoryListInfo();
			categoryListInfo.setId(i);
			categoryListInfo.setName(i+"0");
			categoryListInfos.add(categoryListInfo);
		}
	}

	private void initView(){
		RelativeLayout relativeLayout = relativeLayout(R.id.lv_heard);
		relativeLayout.setBackgroundColor(getResources().getColor(R.color.statusBar));
		showView(R.id.titleback_btn_back, true);
		setTvText(R.id.tv_title, "类目选择");
		findViewById(R.id.titleback_btn_back).setOnClickListener(this);
		tv_category_text = textView(R.id.tv_category_text);
		tv_category_text.setText("一级类目");

		type = getIntent().getIntExtra("type", 0);


		listview = findViewById(R.id.listview);
		categoryListAdapter = new CategoryListAdapter(mContext, categoryListInfos);
		listview.setAdapter(categoryListAdapter);
		listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
				vo = categoryListInfos.get(position);
				for (int i = 0; i < categoryListInfos.size(); i++){
					categoryListInfos.get(i).setShow(false);
				}
				vo.setShow(true);
				categoryListAdapter.notifyDataSetChanged();
				if (vo.getMallCategoryList() != null && vo.getMallCategoryList().size() > 0) {
					startActivityForResult(new Intent(mContext, MyCategoryTwoListActivity.class).putExtra("two", (Serializable) vo), 100);
				}else{

					Bundle bundle = new Bundle();
					bundle.putString("firstCategory", vo.getName());
					bundle.putString("firstCategoryId", vo.getId()+"");
					bundle.putString("twoCategory", "");
					bundle.putString("twoCategoryId", "");
					setResult(6, (new Intent()).putExtra("category", bundle));
					finish();
				}
			}
		});

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if(resultCode!=RESULT_OK)return;
		if(requestCode == 100){

			Bundle twoBundle = data.getBundleExtra("twoCategory");

			Bundle bundle = new Bundle();
			bundle.putString("firstCategory", vo.getName());
			bundle.putString("firstCategoryId", vo.getId()+"");
			bundle.putString("twoCategory", StringUtils.isBlank(twoBundle.getString("twoCategory")) ? "" : twoBundle.getString("twoCategory"));
			bundle.putString("twoCategoryId", (StringUtils.isBlank(twoBundle.getString("twoCategoryId")) || StringUtils.isEquals("0", twoBundle.getString("twoCategoryId"))) ? "" : twoBundle.getString("twoCategoryId"));

//			twoCategory = data.getStringExtra("twoCategory");
			setResult(6, (new Intent()).putExtra("category", bundle));
			finish();
		}
	}

	private void loadData() {

		showProgress("加载中...");

		String url = Config.SBS_ENTITY_MALL_GOOD;
		RequestParams params = new RequestParams();
		params.put("goodBigtype", type);
		params.put("groupId", MyApplication.getmInstance().getLoginMEntity().getGroupId());

		HttpUtil.get(url, params, new HttpUtil.RequestListener() {

			@SuppressWarnings("unchecked")
			@Override
			public void success(String response) {
				disShowProgress();
				try {
					List<CategoryListInfo> datas = new Gson().fromJson(response, new TypeToken<List<CategoryListInfo>>(){}.getType());
					if (datas != null && datas.size() > 0){
						categoryListInfos.clear();
						categoryListInfos.addAll(datas);
						categoryListAdapter.notifyDataSetChanged();
					}
				} catch (Exception e) {
					e.getMessage();
				}
			}

			@Override
			public void failed(Throwable error) {
				disShowProgress();
				showE404();
			}
		});
	}



	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.titleback_btn_back:
				finish();
				break;
			default: break;
		}
	}
	

	
}
